package com.wtf.dsp.admin.core.executor;

import cn.hutool.core.bean.BeanUtil;
import com.wtf.dsp.admin.core.config.WtfDspAdminConfig;
import com.wtf.dsp.admin.core.model.DataSourceInfo;
import com.wtf.dsp.admin.core.model.SqlInfo;
import com.wtf.dsp.admin.core.provider.AbstractMybatisConfigProvider;
import com.wtf.dsp.admin.core.provider.AbstractWtfDynamicDataSourceProvider;
import com.wtf.dsp.admin.enums.SqlMybatisXml;
import com.wtf.dsp.core.enums.SqlType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * date: 2022/6/18 10:59
 * @author linhuanyou
 * @email 496558736@qq.com
 * @version 1.0
 */
@Slf4j
public class WtfDynamicDataSourceMybatisConfigExecutor extends AbstractMybatisConfigProvider {

    private WtfDspAdminConfig dspAdminConfig;

    public WtfDynamicDataSourceMybatisConfigExecutor(WtfDspAdminConfig wtfDspMapperConfig, AbstractWtfDynamicDataSourceProvider dataSourceProvider) {
        super(dataSourceProvider);
        this.dspAdminConfig = wtfDspMapperConfig;
    }

    @Override
    protected List<SqlInfo> getSqls(String ns, String ds) {
        return dspAdminConfig.getWtfDspMapperMapper().getSqls(ns, ds,null,null).stream().map(e->{
            SqlInfo sqlInfo = new SqlInfo();
            BeanUtil.copyProperties(e,sqlInfo);
            if(StringUtils.isNotBlank(e.getSqlId())){
                SqlType sqlType = SqlType.valueOf(e.getSqlType());
                sqlInfo.setSqlType(sqlType);
                sqlInfo.setSqlXml(SqlMybatisXml.obtainSqlstatementXml(sqlType,e.getSqlId(),e.getSqlStatement()));
            }
            return sqlInfo;
        }).collect(Collectors.toList());
    }
}
